package com.roads.site.controller;

import com.roads.common.annotation.Log;
import com.roads.common.constant.UserConstants;
import com.roads.common.core.controller.BaseController;
import com.roads.common.core.domain.AjaxResult;
import com.roads.common.core.page.TableDataInfo;
import com.roads.common.enums.BusinessType;
import com.roads.common.utils.StringUtils;
import com.roads.common.utils.poi.ExcelUtils;
import com.roads.site.domain.SiteLabourBattalion;
import com.roads.site.service.ISiteLabourBattalionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 施工队伍Controller
 * 
 * @author nhp
 * @date 2022-10-25
 */
@RestController
@RequestMapping("/labour/battalion")
public class SiteLabourBattalionController extends BaseController {

    @Autowired
    private ISiteLabourBattalionService service;

    /**
     * 查询列表
     */
    @PreAuthorize("@ss.hasPermi('labour:battalion:list')")
    @GetMapping("/list")
    public TableDataInfo list(SiteLabourBattalion model) {
        startPage();
        List<SiteLabourBattalion> list = service.selectModelList(model);
        return getDataTable(list);
    }

    /**
     * 施工队伍下拉框列表
     *
     * @param institutionCode 标段编号
     * @return 结果
     */
    @GetMapping("/dropDownList")
    public AjaxResult dropDownList(String institutionCode) {
        AjaxResult ajaxResult = new AjaxResult();
        List<SiteLabourBattalion> list = service.dropDownList(institutionCode);
        ajaxResult.put("battalions", list);
        return ajaxResult;
    }

    /**
     * 导出
     */
    @PreAuthorize("@ss.hasPermi('labour:battalion:export')")
    @Log(title = "施工队伍", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SiteLabourBattalion model) {
        List<SiteLabourBattalion> list = service.selectModelList(model);
        ExcelUtils<SiteLabourBattalion> util = new ExcelUtils<SiteLabourBattalion>(SiteLabourBattalion.class);
        return util.exportExcel(list, "施工队伍数据");
    }

    /**
     * 获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('labour:battalion:query')")
    @GetMapping(value = "/{battalionId}")
    public AjaxResult getInfo(@PathVariable("battalionId") Integer battalionId) {
        return AjaxResult.success(service.getModelById(battalionId));
    }

    /**
     * 新增
     */
    @PreAuthorize("@ss.hasPermi('labour:battalion:add')")
    @Log(title = "施工队伍", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SiteLabourBattalion model) {
        if (StringUtils.isNotEmpty(model.getBattalionName())
                && UserConstants.NOT_UNIQUE.equals(service.checkNameUnique(model))) {
            return AjaxResult.error("新增施工队伍失败，队伍名称已存在");
        }
        model.setCreateBy(getUsername());
        return toAjax(service.insert(model));
    }

    /**
     * 修改
     */
    @PreAuthorize("@ss.hasPermi('labour:battalion:edit')")
    @Log(title = "施工队伍", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SiteLabourBattalion model) {
        if (StringUtils.isNotEmpty(model.getBattalionName())
                && UserConstants.NOT_UNIQUE.equals(service.checkNameUnique(model))) {
            return AjaxResult.error("修改施工队伍失败，队伍名称已存在");
        }
        model.setUpdateBy(getUsername());
        return toAjax(service.update(model));
    }

    /**
     * 删除
     */
    @PreAuthorize("@ss.hasPermi('labour:battalion:remove')")
    @Log(title = "施工队伍", businessType = BusinessType.DELETE)
	@DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Integer id) {
        // 检查施工队伍是否使用，如果使用了，就无法删除
        int useCount = service.useCount(id);
        if (useCount > 0) {
            return error("施工队伍已分配劳务工，无法删除。");
        }
        return toAjax(service.delete(id));
    }

    @GetMapping("/downloadTemplate")
    public AjaxResult downloadTemplate() {
        ExcelUtils<SiteLabourBattalion> utils = new ExcelUtils<>(SiteLabourBattalion.class);
        return utils.importTemplateExcel("施工队伍导入模板");
    }


    @PreAuthorize("@ss.hasPermi('labour:battalion:add')")
    @Log(title = "施工队伍", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport, String institutionCode) throws Exception {
        AjaxResult ajaxResult = new AjaxResult();
        ExcelUtils<SiteLabourBattalion> utils = new ExcelUtils<>(SiteLabourBattalion.class);
        try {
            List<SiteLabourBattalion> list = null;
            list = utils.importExcel(file.getInputStream());
            if (StringUtils.isNull(list) || list.size() == 0) {
                ajaxResult.put("status", "error");
                ajaxResult.put("msg", "导入数据不能为空");
                return ajaxResult;
            }
            String userName = getUsername();
            ajaxResult = service.importData(list, updateSupport, userName, institutionCode);
        } catch (Exception e) {
            e.printStackTrace();
            ajaxResult.put("status", "error");
            ajaxResult.put("msg", "excel数据解析失败");
            return ajaxResult;
        }
        return ajaxResult;
    }

}
